using System;
using Cinemachine;
using UnityEditor;
using UnityEngine;
using Yoozoo.Mars.Got;

namespace Gameplay.PVE
{
    public class PveSceneViewer : MonoBehaviour
    {
        public float speed = 1;
        public float cameraSpeed = 50;
        private Transform cameraTrans;
        private CinemachineStateDrivenCamera hanging;
        private GameObject lighting;
        private void Start()
        {
#if UNITY_EDITOR
            var obj = GameObject.Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>(
                "Assets/ResourcesAssets/Pve/Prefabs/Camera/PveCamera.prefab"));
            var hanging = obj.transform.GetChild(0).GetComponent<CinemachineStateDrivenCamera>();
            hanging.Follow = gameObject.transform;
            hanging.LookAt = gameObject.transform;
            hanging.Priority = 100;
            var hangingOffset =  hanging.AddComponent<CinemachineCameraOffset>();
            hangingOffset.m_ApplyAfter = CinemachineCore.Stage.Body;
            hangingOffset.m_Offset = new Vector3(0, 0, -1);
            this.hanging = hanging;
            var camera = obj.GetComponentInChildren<Camera>();
            camera.enabled = true;
            cameraTrans = camera.transform;
#endif
            var root = GameObject.Find("[Root]");
            if (root)
            {
                lighting = root.transform.Find("RPG_Level01_Revise_Settings").gameObject;
            }
            
        }
        
        void Update()
        {
            var offset = Vector3.zero;
            if (Input.GetKey(KeyCode.W))
            {
                offset = speed * Time.deltaTime * (Quaternion.AngleAxis(0,Vector3.up) * Vector3.forward);
            }
            if (Input.GetKey(KeyCode.S))
            {
                offset = speed * Time.deltaTime * (Quaternion.AngleAxis(-180,Vector3.up) * Vector3.forward);
            }
            if (Input.GetKey(KeyCode.A))
            {
                offset = speed * Time.deltaTime * (Quaternion.AngleAxis(-90,Vector3.up) * Vector3.forward);
            }
            if (Input.GetKey(KeyCode.D))
            {
                offset = speed * Time.deltaTime * (Quaternion.AngleAxis(90,Vector3.up) * Vector3.forward);
            }
            var origonForward = new Vector3(offset.x,0,offset.z);
            var angle = cameraTrans.localEulerAngles.y;
            var realForward = Quaternion.AngleAxis(angle, Vector3.up) * origonForward;
            transform.position += realForward;
            transform.forward = realForward;
            if (lighting != null && !lighting.activeInHierarchy)
            {
                lighting.SetActive(true);
            }
        }
    }
}